package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.pcl;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.MatrixToImageWriter;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.services.DeviceService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;
import com.gzhryc.shared_device.pcl.code.dao.db.ChargingCable;
import com.gzhryc.shared_device.pcl.code.dao.enums.ChargingCableEnum;
import com.gzhryc.shared_device.pcl.code.services.ChargingCableService;
import com.gzhryc.shared_device.pcl.code.services.dto.ChargingCableSearch;

public class ChargingCableController {

    static Logger log = Logger.getLogger(ChargingCableController.class);

    @Route("/pcl/device/list.html")
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", ChargingCableEnum.State.values());
        return "/pcl/device/list.html";
    }

    @Route(value = "/pcl/device/list.data")
    public ListResult<ChargingCable> listData(HttpServletRequest request) {
        ListResult<ChargingCable> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        ChargingCableSearch search = WebUtils.getParameter(request, ChargingCableSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            search.setUserId(loginUser.getId());
            result.getBody().setTotal(ChargingCableService.self().countJoinBySearch(search));
            List<ChargingCable> chargingCableList = ChargingCableService.self().findJoinBySearch(search, pageNo, pageLimit);
            if (chargingCableList != null && chargingCableList.size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
                result.getBody().setData(chargingCableList);
            }
        }
        return result;
    }

    @Route("/pcl/device/info.html")
    public String info(HttpServletRequest request) {
        String deviceSn = WebUtils.getString(request,"deviceSn");
        if(StringTools.isNotBlank(deviceSn)){
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    request.setAttribute("device", device);
                    ChargingCable chargingCable = ChargingCableService.self().getJoinByDeviceSn(deviceSn);
                    if (chargingCable != null) {
                        WebUtils.setAttribute(request, chargingCable);
                    }
                }
            }
        }
        return "/pcl/device/info.html";
    }

    @Route(value = { "/pcl/device/qrcode.html" })
    public String qrcode(HttpServletRequest request) {
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringUtils.isNotBlank(deviceSn)) {
            ChargingCable chargingCable = ChargingCableService.self().getByDeviceSn(deviceSn);
            if (chargingCable != null) {
                String httpDomain = PropertyTools.get().getString("httpDomain");
                String url = httpDomain + "/dev/pcl/" + chargingCable.getDeviceSn();
                request.setAttribute("url", url);
            }
        }
        return "/pcl/device/qrcode.html";
    }

    @Route(value = { "/pcl/device/qrcode.jpg" })
    public void qrcodeImg(HttpServletRequest request, HttpServletResponse response) {
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringUtils.isNotBlank(deviceSn)) {
            ChargingCable chargingCable = ChargingCableService.self().getByDeviceSn(deviceSn);
            if (chargingCable != null) {
                try {
                    String httpDomain = PropertyTools.get().getString("httpDomain");
                    String url = httpDomain + "/dev/pcl/" + chargingCable.getDeviceSn();
                    WebUtils.exportJPEG(response);
                    MatrixToImageWriter.writeToStream(url, 400, 300, "jpeg", response.getOutputStream());
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }

    @Route(value = { "/pcl/device/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        if(StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {

                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }
}
